package com.sun.androidexerciseproject.widget.android.test;

import android.Manifest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.sun.androidexerciseproject.widget.R;
import com.sun.androidexerciseproject.widget.permission.XPermission;
import com.sun.androidexerciseproject.widget.util.LocationUtils;
import com.sun.androidexerciseproject.widget.util.LogUtil;

public class Lesson1Activity extends AppCompatActivity {
    private Button mToast;
    private Button mCount;
    private TextView mText;
    private int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lessson1);
        mToast = findViewById(R.id.btn_toast);
        mCount = findViewById(R.id.btn_count);
        mText = findViewById(R.id.tv_test);
        mToast.setOnClickListener(v -> Toast.makeText(Lesson1Activity.this, getString(R.string.android_lesson1_title), Toast.LENGTH_SHORT).show());
        mCount.setOnClickListener(v -> {
            count++;
            mText.setText(String.valueOf(count));

        });
        mText.setText(String.valueOf(count));
        doPermission();
    }

    private void getLocation() {
        String location = LocationUtils.getInstance().getLocations(this);
        LogUtil.log("getLocation location = " + location);
    }

    /**
     * 动态请求地理位置权限
     */
    private void doPermission() {
        XPermission.requestPermissions(this, 101, new String[]{
                Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
                new XPermission.OnPermissionListener() {
            @Override
            public void onPermissionGranted() {
                getLocation();
            }

            @Override
            public void onPermissionDenied() {
                XPermission.showTipsDialog(Lesson1Activity.this);
            }
        });
    }

}
